package com.ruoyi.web.controller.system;

import java.text.ParseException;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.system.domain.dto.ImUserListDto;
import com.ruoyi.system.domain.dto.ImUserPasswordDto;
import com.ruoyi.system.domain.dto.ImUserUpdateDto;
import com.ruoyi.system.domain.vo.ImUserDetailVo;
import io.swagger.models.auth.In;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.ImUser;
import com.ruoyi.system.service.IImUserService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 【imuser】Controller
 *
 * @author ruoyi
 * @date 2024-03-30
 */
@RestController
@RequestMapping("/system/imUser")
public class ImUserController extends BaseController
{
    @Autowired
    private IImUserService imUserService;

    /**
     * 查询im用户列表
     */
    @PreAuthorize("@ss.hasPermi('system:user:list')")
    @GetMapping("/list")
    public TableDataInfo list(ImUser imUser)
    {
        startPage();
        List<ImUser> list = imUserService.selectImUserList(imUser);
        return getDataTable(list);
    }

    /**
     * 导出im用户列表
     */
    @PreAuthorize("@ss.hasPermi('system:user:export')")
    @Log(title = "im用户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ImUser imUser)
    {
        List<ImUser> list = imUserService.selectImUserList(imUser);
        ExcelUtil<ImUser> util = new ExcelUtil<ImUser>(ImUser.class);
        util.exportExcel(response, list, "im用户数据");
    }

    /**
     * 获取im用户详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:user:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(imUserService.selectImUserById(id));
    }

    /**
     * 新增im用户
     */
    @PreAuthorize("@ss.hasPermi('system:user:add')")
    @Log(title = "im用户", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ImUser imUser)
    {
        return toAjax(imUserService.insertImUser(imUser));
    }

    /**
     * 修改im用户
     */
    @PreAuthorize("@ss.hasPermi('system:user:edit')")
    @Log(title = "im用户", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ImUser imUser)
    {
        return toAjax(imUserService.updateImUser(imUser));
    }

    /**
     * 删除im用户
     */
    @PreAuthorize("@ss.hasPermi('system:user:remove')")
    @Log(title = "im用户", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(imUserService.deleteImUserByIds(ids));
    }

    /**
     * 新增用户
     * @param imUser
     * @return
     */
    @PostMapping("/addUser")
    public AjaxResult addImUser(@RequestBody ImUser imUser){
        return imUserService.addImUser(imUser);
    }


    /**
     * 用户列表
     * @param listDto
     * @return
     */
    @GetMapping("/userList")
    public TableDataInfo userList(ImUserListDto listDto) throws ParseException {
        return imUserService.userList(listDto);
    }


    /**
     * 用户详情
     * @param id
     * @return
     */
    @GetMapping("/userDetail/{id}")
    public AjaxResult userDetail(@PathVariable Long id){
        ImUser imUser = imUserService.getById(id);
        ImUserDetailVo imUserDetailVo = BeanUtil.toBean(imUser, ImUserDetailVo.class);
        return AjaxResult.success(imUserDetailVo);
    }

    /**
     * 修改
     * @param updateDto
     * @return
     */
    @PostMapping("/updateUser")
    public AjaxResult updateUser(@RequestBody ImUserUpdateDto updateDto){

        imUserService.updateUser(updateDto);
        return AjaxResult.success();
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @PostMapping("/deleteUser/{id}")
    public AjaxResult deleteUser(@PathVariable Long id){
        imUserService.deleteUser(id);
        return AjaxResult.success();
    }

    /**
     * 重置密码
     * @param passwordDto
     * @return
     */
    @PostMapping("/userPassword")
    public AjaxResult userPassword(@RequestBody ImUserPasswordDto passwordDto){
        imUserService.userPassword(passwordDto);
        return AjaxResult.success();
    }

    /**
     * 首页
     * @return
     */
    @PostMapping("/homePage")
    public AjaxResult homePage(){
        Map<String, Integer> map=imUserService.homePage();
        return AjaxResult.success(map);

    }

    /**
     * 清除聊天记录
     * @return
     */
    @PostMapping("/deleteContact")
    public AjaxResult deleteContact(){
        imUserService.deleteContract();
        return AjaxResult.success();
    }


}
